package com.example.studentmanagersystem;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.fs.adapters.StuSearchGradeAdapter;
import com.fs.po.StuSocre;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

/**
 * @version 1.0
 * @author cwf
 * data 2015-11-12
 *
 */
public class StudentSearchGradeActivity extends Activity implements OnClickListener {

	
	
	private ListView gradeList;
	
	private List<StuSocre> datalist;
	
	private StuSearchGradeAdapter searchGradeAdapter;
	
	String name,password;
	
	private Button actionBack,actionMenu;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.student_search_grade_activity);
		
		this.getLoginInfo();
		
		this.init();
		
		this.setgradeAdapter();
		
		this.getGradeData("stu_get_grade");
	}
	
	/**
	 * get UserName and Password
	 */
	private void getLoginInfo() {
		
		this.name=MainActivity.username;
		this.password=MainActivity.password;
		
		Toast.makeText(StudentSearchGradeActivity.this, name, 0).show();
	}
	
	
	/**
	 * init widget
	 */
	private void init() {
		
		this.gradeList=(ListView) this.findViewById(R.id.stu_select_grade_ListView);
		this.actionBack=(Button) this.findViewById(R.id.stuGrade_action_backButton);
		this.actionMenu=(Button) this.findViewById(R.id.stuGrade_action_menu);
		
		//back button
		this.actionBack.setOnClickListener(this);
		//menu listener
		this.actionMenu.setOnClickListener(this);
	}
	
	/**
	 * set Adapter
	 */
	private void setgradeAdapter() {
		
		this.searchGradeAdapter=new StuSearchGradeAdapter(this);
		
		this.gradeList.setAdapter(searchGradeAdapter);

	}
	
	/**
	 * get data from service
	 */
	
	private void getGradeData(String action) {

		datalist=new ArrayList<StuSocre>();
		
		RequestParams params = new RequestParams();
		
		
		params.addBodyParameter("action", action);
		
		params.addBodyParameter("_id", ""+MainActivity._id);
		
//		params.addBodyParameter("name", this.name);
//		params.addBodyParameter("password", this.password);
		
		HttpUtils httpUtils=new HttpUtils();
		
		httpUtils.send(HttpMethod.POST, URL.SOCRE_URL, params, new RequestCallBack<String>() {

			@Override
			public void onSuccess(ResponseInfo<String> responseInfo) {

				String jsonString=responseInfo.result;
				
				Toast.makeText(StudentSearchGradeActivity.this, jsonString, 0).show();
				
				setSocre(jsonString);
				 
			}

			@Override
			public void onFailure(HttpException error, String msg) {
				
			}

		});
	}
	
	

	/**
	 * json data
	 * @param jsonString
	 */
	private void setSocre(String jsonString) {
		
		StuSocre ssocre=null;
		if(jsonString!=null){
			
			try {
				JSONObject jsonObject = new JSONObject(jsonString);
				JSONArray array=jsonObject.getJSONArray("class_score");
				for(int i=0;i<array.length();i++){
					JSONObject json=array.getJSONObject(i);
					
					String className=json.getString("class_name");
					
					int score=Integer.parseInt(json.getString("score"));
					
					String pj=json.getString("pj");
					 
					ssocre=new StuSocre(className,score,0,0,0,pj);
					
					datalist.add(ssocre);
				}
				
				this.searchGradeAdapter.setData(datalist);
//				
				this.searchGradeAdapter.notifyDataSetChanged();
			} catch (JSONException e) {
				e.printStackTrace();
			}
			
		}
		
	}

	@Override
	public void onClick(View v) {
		
		switch (v.getId()) {
		
		case R.id.stuGrade_action_backButton:
			
			this.finish();
			
			break;
			
		case R.id.stuGrade_action_menu:
			
			PopupMenuUtil popupMenuUtil=new PopupMenuUtil(v,this);
			popupMenuUtil.actionBackListener();
			
			break;
		default:
			break;
		}
		
	}
	
	
	
	
	
	
	
}
